package com.deparse.mydialog.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/7/21.
 */
public class ShaowTextView extends TextView {
    private GradientDrawable mShadowDrawable;
    private int mShadowHeight;

    public ShaowTextView(Context context) {
        super(context);
        init(context);
    }


    public ShaowTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ShaowTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        if (mShadowDrawable == null) {
            mShadowDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                    new int[]{Color.parseColor("#ffa0a0a0"), Color.parseColor("#50a0a0a0"), Color.parseColor("#00a0a0a0")});
            mShadowHeight = (int) (8 * getResources().getDisplayMetrics().density);
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        // prepare variables
        int pLeft = 5;
        int pTop = 5;

        // draw child
        canvas.save();

        canvas.clipRect(pLeft, pTop, pLeft + 5, pTop + 5);

        canvas.translate(pLeft, pTop + 5);

        mShadowDrawable.setBounds(5,
               5,
               5,
               5 + mShadowHeight);
        mShadowDrawable.draw(canvas);

        canvas.restore();
    }
}
